//
//  EasyToTescoNewsController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/9/25.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "EasyToTescoNewsController.h"
#import "EasyNewsModel.h"
#import "EasyToTescoNewsDetailsVc.h"

@interface EasyToTescoNewsController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSUInteger refreshIndex;
    MBProgressHUD *progressHUD;
}

@property(nonatomic,strong)NSMutableArray *EasyNewsArray;

@property(nonatomic,strong)UITableView *easyNewsView;

@end

@implementation EasyToTescoNewsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    refreshIndex = 1;
    [self addTableView];
    [self easyToTescoNewsLoadData];
    [self addPullUpdateData];
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)addTableView
{
    
    UITableView *easyNewsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    self.easyNewsView = easyNewsView;
    [self.view addSubview:easyNewsView];
    self.easyNewsView.rowHeight = HEIGHT/12;
    easyNewsView.backgroundColor = [UIColor whiteColor];
    easyNewsView.delegate = self;
    easyNewsView.dataSource = self;
}

#pragma mark >>>>>>>>刷新
- (void)addPullUpdateData
{
    self.easyNewsView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(BusinessDetailsLoadMoreData)];
    
}

- (void)BusinessDetailsLoadMoreData
{
    refreshIndex ++;
    [self easyToTescoNewsLoadData];
    [self.easyNewsView.mj_footer endRefreshing];
}

- (void)easyToTescoNewsLoadData
{
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    
    NSDictionary *params = @{
                             @"f_id":@"1",
                             @"p":[NSString stringWithFormat:@"%zd",refreshIndex]
                             };
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/News/Fast" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            
            NSArray *arr = [object objectForKey:@"data"];
            for (NSDictionary *tempDict in arr) {
                EasyNewsModel *easyNewsModel = [[EasyNewsModel alloc] initWithDictionary:tempDict error:nil];
                if (easyNewsModel) {
                    [self.EasyNewsArray addObject:easyNewsModel];
                }
                
            }
        }else{
            
            [self.easyNewsView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.easyNewsView reloadData];
        [progressHUD hide:YES];
        
    }];
}

#pragma mark 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.EasyNewsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier=@"easyNewsCell";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    
    EasyNewsModel *easyNewsModel = self.EasyNewsArray[indexPath.row];
    cell.textLabel.text = easyNewsModel.title;
    cell.textLabel.font = FONT(15);
    
    cell.detailTextLabel.text = easyNewsModel.date;
    cell.detailTextLabel.textColor = [UIColor grayColor];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EasyToTescoNewsDetailsVc *newsDetailsVC = [[EasyToTescoNewsDetailsVc alloc] init];
    EasyNewsModel *easyNewsModel = self.EasyNewsArray[indexPath.row];
    newsDetailsVC.link = easyNewsModel.link;
    newsDetailsVC.title = easyNewsModel.title;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:newsDetailsVC];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}



- (NSMutableArray *)EasyNewsArray
{
    if (!_EasyNewsArray) {
        _EasyNewsArray = [NSMutableArray array];
    }
    return _EasyNewsArray;
}


@end
